package com.banciyuan.bcywebview.biz.main.newdiscover.index.discoverholder;

import android.content.Context;
import android.text.Html;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.banciyuan.bcywebview.R;
import com.banciyuan.bcywebview.base.helper.timelinedatacenter.DetailDataHelper;
import com.banciyuan.bcywebview.base.view.textview.EllipsezeTextView;
import com.banciyuan.bcywebview.utils.universalimageloader.core.ImageLoader;
import com.banciyuan.bcywebview.utils.universalimageloader.utils.ImageOptions;

import java.util.List;

import de.greenrobot.daoexample.model.TagDetail;
import de.greenrobot.daoexample.model.Timeline;

/**
 * Created by squall on 2017/3/22.
 */

public class NovelDiscoverHolder extends BaseDiscoverHolder {

    private ImageView ivNovelCover;
    private TextView tvNovelTitle;
    private ImageView tvNovelStatus;
    private TextView tvNovelContent;
    private TextView ellipsis;
    private EllipsezeTextView intro;
    private TextView tvNovelTags;

    public NovelDiscoverHolder(View itemView) {
        super(itemView);
        ivNovelCover = (ImageView) itemView.findViewById(R.id.write_cover);
        tvNovelTitle = (TextView) itemView.findViewById(R.id.write_title);
        tvNovelStatus = (ImageView) itemView.findViewById(R.id.write_serial_status);
        tvNovelContent = (TextView) itemView.findViewById(R.id.write_content);
        ellipsis = (TextView) itemView.findViewById(R.id.ellipsis);
        intro = (EllipsezeTextView) itemView.findViewById(R.id.ground_intro_tv);
        tvNovelTags = (TextView) itemView.findViewById(R.id.tv_write_tags);
    }

    @Override
    public void renderData(final Timeline mItem, final Context context) {
        super.renderData(mItem, context);
        ImageLoader.getInstance().displayImage(mItem.getContentAddress(), ivNovelCover, ImageOptions.options);
        if ("true".equals(mItem.getPost_in_set())) {
            tvNovelStatus.setImageResource(R.drawable.timeline_serise);
        } else {
            tvNovelStatus.setImageResource(R.drawable.timeline_single_write);
        }
        if (!TextUtils.isEmpty(mItem.getSummary())) {
            tvNovelContent.setText(Html.fromHtml(mItem.getSummary()).toString().trim().replaceAll("(\\n)+", "\n"));
        }
        if (!TextUtils.isEmpty(mItem.getTitle())) {
            tvNovelTitle.setText(mItem.getTitle());
        }
        setRecommendIntro(context, intro, mItem, mItem.getIntro(), context.getString(R.string.post_write));
        itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DetailDataHelper.gotoWriterDetail(context, mItem.getRp_id(), mItem.getWp_id(), false);
            }
        });
        intro.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DetailDataHelper.gotoWriterDetail(context, mItem.getRp_id(), mItem.getWp_id(), false);
            }
        });
        List<TagDetail> tags = mItem.getTags();
        if (null != tags && !tags.isEmpty()) {
            tvNovelTags.setVisibility(View.VISIBLE);
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < tags.size(); i++) {
                sb.append(tags.get(i).getTag_name());
                if (i < tags.size() - 1) {
                    sb.append(" | ");
                }
            }
            tvNovelTags.setText(sb.toString());
        } else {
            tvNovelTags.setVisibility(View.GONE);
        }
    }

    private void setRecommendIntro(Context mContext, EllipsezeTextView tvIntro, Timeline mItem, String intro, String defaultIntro) {
        String finalIntro = TextUtils.isEmpty(intro) ? defaultIntro : intro;
        finalIntro = String.format(mContext.getResources().getString(R.string.card_recommended_user_format), mItem.getOUName(), finalIntro);
        tvIntro.setText(finalIntro, mItem.getOUName(), mItem.getOuid(), true);
        tvIntro.setVisibility(View.VISIBLE);
    }
}
